package com.ww.springboot.boot.algorithm.string;


import com.sun.org.apache.xpath.internal.operations.Bool;
import org.apache.commons.lang3.StringUtils;

import java.util.Arrays;
import java.util.List;

/**
 * 将一串字符的数字/字母位置改为用十进制表示
 *
 * @author wanwei
 * @since 2021-03-19 17:22
 */
public class PostCodeDemo {

    private static final List<Character> list = Arrays.asList('A', 'B', 'C', 'D', 'E', 'F', 'G',
            'H', 'I', 'J', 'K', 'L', 'M', 'N',
            'O', 'P', 'Q', 'R', 'S', 'T',
            'U', 'V', 'W', 'X', 'Y', 'Z');

    private static final char min = '!';
    private static final char max = '~';
    private static final char middle = '|';

    //将邮编转化为字符串
    public static void main(String[] args) {
        System.out.println("邮编是否匹配:" + compare("AB", "AB12", "AB14"));
    }

    private static Boolean compare(String postcode, String start, String end) {
        String ss1 = buildBegin(start);
        String ss2 = buildPostCode(postcode);
        String ss3 = buildEnd(end);

        System.out.println(ss1);
        System.out.println(ss2);
        System.out.println(ss3);

        if (ss3.compareTo(ss1) < 0) {
            throw new RuntimeException("邮编范围有误");
        }

        if (ss2.length() < ss1.length() - 1
                || ss2.length() < ss3.length() - 1) {
            return false;
        }

        if (ss2.compareTo(ss1) >= 0
                && ss3.compareTo(ss2) >= 0) {
            return true;
        }
        return false;
    }

    private static String buildBegin(String postcode) {
        return buildPostCode(postcode, true);
    }

    private static String buildEnd(String postcode) {
        return buildPostCode(postcode, false);
    }

    private static String buildPostCode(String postcode) {
        return buildPostCode(postcode, null);
    }

    private static String buildPostCode(String postcode, Boolean isBegin) {
        postcode = postcode.trim().toUpperCase();
        String postcodeNew = "";
        String num = "";
        for (int i = 0; i < postcode.length(); i++) {
            char ch = postcode.charAt(i);
            if (Character.isLetter(ch)) {
                if (StringUtils.isNotBlank(num)) {
                    postcodeNew += changeToString(Integer.valueOf(num));
                    num = "";
                }
                postcodeNew += ch;
            } else if (Character.isDigit(ch)) {
                num += ch;
            } else if (ch == ' ') {
                if (StringUtils.isNotBlank(num)) {
                    postcodeNew += changeToString(Integer.valueOf(num));
                    num = "";
                }
                postcodeNew += "**";
            } else {
                throw new RuntimeException("字符");
            }

            if (i == postcode.length() - 1
                    && StringUtils.isNotBlank(num)) {
                postcodeNew += changeToString(Integer.valueOf(num));
            }
        }

        if (isBegin == null) {
            return postcodeNew + middle;
        }

        if (isBegin) {
            return postcodeNew + min;
        } else {
            return postcodeNew + max;
        }
    }

    //将数字转化为AAAAA
    private static String changeToString(int i) {
        String ss = "";
        while (i > 0) {
            int l2 = i % 26;
            i = i / 26;
            ss = list.get(l2) + ss;
        }
        return "#" + addString(ss, 5, "A") + "#";
    }


    /**
     * @param s     字符串
     * @param index 位数
     * @param flag  需要填补的字符
     * @return
     */
    private static String addString(String s, int index, String flag) {
        while (s.length() < index) {
            s = flag + s;
        }
        return s;
    }
}
